<template>
  <div :class="className"
       :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from "echarts"

export default {
  name: "Histogram",
  props: {
    className: {
      type: String,
      default: "chart"
    },
    headTitle: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "350px"
    },
    chartData: {
      type: Array,
      required: true
    },
    chartTime: {
      type: Array,
      required: true
    },
    min: {
      type: String,
      required: true
    },
    max: {
      type: String,
      required: true
    },
    interval: {
      type: Number,
      required: true
    },
    name: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(
          this.headTitle,
          this.chartData,
          this.chartTime,
          this.min,
          this.max,
          this.interval,
          this.name
        );
      }
    },
    chartTime: {
      deep: true,
      handler(val) {
        this.setOptions(
          this.headTitle,
          this.chartData,
          this.chartTime,
          this.min,
          this.max,
          this.interval,
          this.name
        );
      }
    }
  },
  mounted() {
    window.addEventListener("resize", this.resizeHandler);
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeHandler);
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    resizeHandler() {
      this.chart.resize();
    },
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(
        this.headTitle,
        this.chartData,
        this.chartTime,
        this.min,
        this.max,
        this.interval,
        this.name
      );
    },
    setOptions(title, chartData, chartTime, min, max, interval, name) {
      this.chart.setOption({
        color: ["#1890FF"],
        title: {
          text: title,
          left: "center",
          textStyle: {
            fontSize: 14
          }
        },
        grid: {
          left: 20,
          right: 20,
          bottom: 0,
          top: 50,
          containLabel: true
        },
        legend: {
          data: [name],
          top: "bottom"
        },
        calculable: true,
        xAxis: [
          {
            type: "category",
            data: chartTime.map(function (item) {
              return item.substring(0, 5);
            }),
            axisTick: {
              show: false
            },
            axisPointer: {
              label: {
                fontSize: "12px"
              }
            }
          }
        ],
        yAxis: [
          {
            type: "value",
            name: "单位：%",
            nameLocation: "end",
            min: min,
            max: max,
            //interval: 20
            boundaryGap: false,
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: true
            }
          }
        ],
        series: [
          {
            //name: name,
            type: "bar",
            showBackground: true,
            backgroundStyle: {
              color: "rgba(180, 180, 180, 0.2)"
            },
            data: chartData.map(function (item) {
              return (item * 100).toFixed(0);
            }),
            barWidth: 20,
            label: {
              position: "top",
              show: true,
              color: "#000",
              formatter: `\n{c}%`
            }
          }
        ]
      });
    }
  }
};
</script>

<style scoped>
</style>
